﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Midgard
{
    class Player
    {
        public int health;
        Vector2 position, projectileVelocity;
        Texture2D sprite, projectileSprite;
        bool isActive;
        //int screenWidth;
        const float playerSpeed = 10;
        const int PLAYER_MID = 253;
        int projectileDamage;

        TimeSpan fireTime;
        TimeSpan previousFireTime;
        List<Projectile> projectiles; // Array list of projectiles

        // Keyboard states used to determine key presses.
        KeyboardState currentKeyboardState;
        KeyboardState previousKeyBoardState;

        // Gamepad states used to determine button presses.
        GamePadState currentGamePadState;
        GamePadState previousGamePadState;

        /* Used for animation purposes */
        int height = 87;
        int frame, delay, currDelay;
        Rectangle destRect, sourceRect, destRect2;
        bool reverse;


        Rectangle viewportRect;

        public int Width
        {
            get { return sprite.Width; }
        }

        public bool IsActive
        {
            get { return isActive; }
        }

        public int Health
        {
            get{return health;}
        }

        public Vector2 Position
        {
            get { return position; }
            
        }

        public float PlayerSpeed
        {
            get { return playerSpeed; }
        }

        public Player(Vector2 position, Texture2D sprite, Rectangle viewport, 
            Texture2D ProjectileSpriteTexture)
        {
            health = 100;
            this.position = position;
            isActive = true;
            this.sprite = sprite;
            viewportRect = viewport;
            fireTime = TimeSpan.FromSeconds(.15f);
            projectiles = new List<Projectile>();

            projectileSprite = ProjectileSpriteTexture;
            projectileVelocity = new Vector2(0, -7);
            projectileDamage = 1;

            /* Used for animation */
            frame = 0;
            destRect = new Rectangle((int)position.X, (int)position.Y, sprite.Width, height);
            sourceRect = new Rectangle(0, 0, sprite.Width, height);
            destRect2 = new Rectangle((int)position.X - Width, (int)position.Y, Width, height);
            delay = 6;
            reverse = false;
        }

        public void update(GameTime gameTime)
        {
            // Save the previous state.
            previousGamePadState = currentGamePadState;
            previousKeyBoardState = currentKeyboardState;

            // Read the current state.
            currentKeyboardState = Keyboard.GetState();
            currentGamePadState = GamePad.GetState(PlayerIndex.One);

            // Checks the health
            if (health <= 0)
                isActive = false;

            /* I reset the position based if I go out of the boundary */
            if (position.X < 0)
            {
                position.X = position.X + Width;
            }
            if (position.X > viewportRect.Width)
            {
                position.X = position.X - Width;
            }

            // Get Thumbstick Controls
            position.X += currentGamePadState.ThumbSticks.Left.X * PlayerSpeed;

            // Use the Keyboard or Dpad
            if (currentKeyboardState.IsKeyDown(Keys.Left) || currentGamePadState.DPad.Left == ButtonState.Pressed)
            {
                position.X -= PlayerSpeed;
            }

            if (currentKeyboardState.IsKeyDown(Keys.Right) || currentGamePadState.DPad.Right == ButtonState.Pressed)
            {
                position.X += PlayerSpeed;
            }


            /* If the Space bar key is pressed, the Serpent will fire */
            // Fire only every interval we set as the fireTime
            if (currentKeyboardState.IsKeyDown(Keys.Space) && gameTime.TotalGameTime - previousFireTime > fireTime)
            {
                // Reset our current time
                previousFireTime = gameTime.TotalGameTime;

                // Add the projectile, but add it to the front and center of the player
                //259 is where the eye is
                projectiles.Add(new Projectile(projectileSprite, new Vector2(position.X + 259, position.Y), 
                    projectileVelocity, viewportRect, projectileDamage, 0.1f, Projectile.Faction.player));
            }

            // Update the Projectiles
            for (int i = projectiles.Count - 1; i >= 0; i--)
            {
                projectiles[i].Update();

                if (projectiles[i].active == false)
                {
                    projectiles.RemoveAt(i);
                }
            }


            /* Should update the source and destination rectangles as well */
            destRect = new Rectangle((int)position.X, (int)position.Y, Width, height);
            sourceRect = new Rectangle(0, height * frame, sprite.Width, height);
            destRect2 = new Rectangle((int)position.X - Width, (int)position.Y, Width, height);
            currDelay++;

            if (currDelay > delay)
            {
                if (reverse)
                {
                    frame = (frame - 1);
                    if (frame == 0)
                        reverse = false;
                }
                else
                {
                    frame = (frame + 1) % 4;
                    if (frame == 3)
                        reverse = true;
                }



                currDelay = 0;
            }
        }


        public void draw(SpriteBatch spriteBatch)
        {

            if (isActive)
                spriteBatch.Draw(sprite, destRect, sourceRect, Color.White);


                spriteBatch.Draw(sprite, destRect2, sourceRect, Color.White);

                for (int i = 0; i < projectiles.Count; i++)
                {
                    projectiles[i].Draw(spriteBatch);
                }
        }
    }
}
